#!/usr/bin/env python3
# -*- coding: utf-8 -*-
__author__ = "jzp";

import requests;
from bs4 import BeautifulSoup;
import re;
from entity.shuangseqiu import *;
from datetime import datetime;

'''
双色球爬虫
http://kaijiang.zhcw.com/zhcw/html/ssq/list.html
http://www.sfac.xyz/economy/lottery
http://538b537e25.zicp.vip:13595/api/economy/bicolorSpheres
'''

headers = {
    "Token":"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJhdWQiOiIxIiwicm9sZSI6IkFkbWluIiwidXNlckltYWdlIjoiaHR0cDovL3d3dy5zZmFjLnh5ejo4MDAwL2ltYWdlcy9wcm9maWxlLzE2NzAwNzA0NzMwNTUuanBnIiwiaWQiOjEsImV4cCI6MTY4Mzg5ODk2OCwidXNlck5hbWUiOiJhZG1pbiIsImlhdCI6MTY4MzgxMjU2OH0.jdx_Y31cpaSRe-3TBNgAcq8gG8a9IKmQ1YYIRM-TfcQ",
    "User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/113.0"
}

# 获取所有数据
def get_all_data(url, page_count):
    all_data = [];
    for page in range(1, page_count + 1):
        page_data_list = dcb_page_data(url, page);
        all_data += page_data_list;

    return all_data;

# 获取双色球某页数据
def dcb_page_data(url, current_page=1):
    page_data_list = [];

    data = {"currentPage":current_page, "direction":"desc","keyword":"","pageSize":10,"sort":"issue_no"};
    r = requests.post(url=url, headers=headers, json=data);
    print(r.status_code);
    if r.status_code == 200:
        r.encoding = r.apparent_encoding;
        results = r.json().get("list");

        for result in results:
            dcb = Double_Color_Ball(
                issue_no = result.get("issueNo", ""),
                award_ate = result.get("awardDate"),
                red_ball = result.get("redBall", ""),
                blue_ball = result.get("blueBall", ""),
                total_sales = result.get("totalSales", 0),
                first_prize_number = result.get("firstPrizeNumber", 0),
                second_prize_number = result.get("secondPrizeNumber", 0),
                create_date = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
            );
            print(dcb.__dict__);
            page_data_list.append(dcb);

        return page_data_list;

if __name__ == '__main__':
    url = "http://538b537e25.zicp.vip:13595/api/economy/bicolorSpheres";
    # dcb_page_data(url);
    get_all_data(url, 10);